package com.yds.developtools.utils

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.*
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import android.net.wifi.WifiManager
import android.os.Build
import android.provider.Settings
import androidx.annotation.RequiresApi

/**
 * 作者:     YDS
 * 创建时间: 2023/5/29 15:27
 * 页面描述: 网络状态-帮助类
 */
@Suppress("unused")
object YDSNetworkWifiStrengthHelper {

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    @SuppressLint("MissingPermission")
    fun Context.initNetworkAvailable(
        isAlert: Boolean = false,
        onNetConnectListener: OnNetConnectListener
    ) {
        val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val networkRequest = NetworkRequest.Builder()
            .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
            .build()
        connectivityManager.registerNetworkCallback(networkRequest, object : ConnectivityManager.NetworkCallback() {
            override fun onAvailable(network: Network) {
                // 网络连接可用
                val networkCapabilities = connectivityManager.getNetworkCapabilities(network)
                if (networkCapabilities != null && networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
                    val wifiStrength = getWifiStrength()
                    val result = when (wifiStrength) {
                        0 -> "信号强度: 无信号"
                        1 -> "信号强度: 非常弱"
                        2 -> "信号强度: 较弱"
                        3 -> "信号强度: 良好"
                        4 -> "信号强度: 强"
                        else -> {
                            "信号强度: 丢失"
                        }
                    }
                    onNetConnectListener.onNetAvailable(network, result)
                    if (wifiStrength != -1) {
                        // 这里可以处理获取到的 Wi-Fi 信号强度
                        YDSLogHelper.e("Wi-Fi 信号强度: $wifiStrength")
                    }
                }
            }

            override fun onLost(network: Network) {
                // 网络连接断开
                onNetConnectListener.onNetLost(network)
                // 是否提示网络断开
                if (isAlert) networkAlert()
            }
        })
    }

    @SuppressLint("WifiManagerLeak")
    private fun Context.getWifiStrength(): Int {
        val wifiManager = getSystemService(Context.WIFI_SERVICE) as WifiManager
        val wifiInfo = wifiManager.connectionInfo
        return if (wifiInfo != null) {
            // 计算信号强度，这里将信号强度分为 0 - 4 五个等级
            WifiManager.calculateSignalLevel(wifiInfo.rssi, 5)
        } else {
            -1
        }
    }

    /*** 作者: YDS 时间: 2023/5/29 15:26 描述: 网络监听 ***/
    interface OnNetConnectListener {
        fun onNetAvailable(network: Network, wifiStrength: String)
        fun onNetLost(network: Network)
    }

    /*** 作者: YDS 时间: 2023/5/29 15:52 描述: 网络断开连接提示 ***/
    private fun Context.networkAlert() {
        val builder = AlertDialog.Builder(this)
        builder.setTitle("提示：网络异常").setMessage("是否对网络进行设置?")
        builder.setPositiveButton("是") { dialog, _ ->
            Intent(Settings.ACTION_WIFI_SETTINGS).apply {
                startActivity(this)
            }
            dialog.dismiss()
        }
        builder.setNegativeButton("否") { dialog, _ -> dialog.cancel() }
        builder.show()
    }
}